<?php 
	//model类的实现
	header('content-type:text/html;charset=utf-8');
	class Model extends PDO{
		//成员属性
		public $tableName;//表名
		private $pk = '';//主键字段名
		private $fields = array();//除了主键的字段数组
		private $field = '';//字段信息
		private $where = '';//条件子句
		private $limit = '';//分页设置
		private $order = '';//排序子句

		//构造方法
		public function __construct($tableName){
			parent::__construct("mysql:host=".HOST.";dbname=".DBNAME.";charset=".CHARSET, USER, PASS);
			//表名的存储
			$this->tableName = $tableName;
			// $this->pdo = new PDO("mysql:host=".HOST.";dbname=".DBNAME.";charset=".CHARSET, USER, PASS) or die('数据库连接失败');
			$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
			//调用方法
			$this->getAllFields();
		}

		/**
		* 获取字段信息
		*/
		private function getAllFields(){
			//获取表的所有的字段信息
			$sql = 'desc '.$this->tableName;
			//发送sql
			$result = $this->query($sql);
			//遍历
			$d = array();
			$pk = '';
			while($row = $result->fetch()){
				if($row['Key'] == 'PRI'){
					$pk = $row['Field'];
				}else{
					//检测正在遍历的字段是否为主键
					$d[] = $row['Field'];
				}
			}
			$this->pk = $pk;
			$this->fields = $d;
		}

		/**
		* 检测数据
		*/
		private function filterData($data){
			//检测字段合法性
			// var_dump($this->fields);die;
			foreach($data as $k=>$v){
				if(!in_array($k, $this->fields) && $k != $this->pk){
					if(MODE == 1){
						die('数据不合法,不合法的字段为'.$k);
					}
					if(MODE == 2){
						unset($data[$k]);
					}
				}
			}
			//返回结果
			return $data;
		}

		/**
		* 添加操作
		*/
		public function add($data){
			//过滤检测
			$data = $this->filterData($data);
			//拼接字段名的sql语句
			$keys = array_keys($data);// ['username','password']  => username,password
			$keys_sql = implode(',',$keys);
			//获取所有的键值
			$values = array_values($data); //  ['admin','admin']=>  "admin","admin"
			$values_sql = '"'.implode('","',$values).'"';
			//准备sql语句
			$sql = 'insert into '.$this->tableName.' ('.$keys_sql.')values('.$values_sql.')';
			//发送sql语句
			$result = $this->exec($sql);
			//判断
			if($result){
				return $this->lastInsertId();
			}else{
				return false;
			}
		}

		/**
		* 删除
		*/
		public function delete($id){
			//拼接sql语句
			$sql = 'delete from '.$this->tableName.' where '.$this->pk.' = '.$id;
			//发送sql指令
			$result = $this->exec($sql);
			//提取结果
			if($result){
				return $result;
			}else{
				return false;
			}
		}

		/**
		* 修改操作
		*/
		public function save($arr){
			//过滤非法的字段信息
			$arr = $this->filterData($arr);
			//遍历数组
			$set = '';//使用变量的时候  一定要养成习惯 先声明后使用
			$where = '';
			foreach($arr as $k=>$v){
				//拼接没字符串语句
				if($k == $this->pk){
					$where = " where ".$this->pk." = ".$v;
				}else{
					$set .= $k."='".$v."',";
				}
			}
			//检测是否有条件
			if($where == ''){
				die('对不起 不允许没有条件的更新操作;');
				return false;
			}

			//清除两侧的 ','
			$set_sql = trim($set, ',');
			//[拼接sql语句]
			$sql = "update ".$this->tableName." set ".$set_sql. $where;
			//发送sql指令
			$result = $this->exec($sql);
			//检测
			if($result){
				return $result;
			}else{
				return false;
			}
		}

		/**
		* 查询单条数据
		*/
		public function find($id){
			//sql拼接
			$sql = "select * from ".$this->tableName." where ".$this->pk." = ".$id;
			//发送sql指令
			$result = $this->query($sql);
			//提取结果集
			if($result){
				return $result->fetch();
			}else{
				return false;
			}
		}

		/**
		* 查询多条数据
		*/
		public function select(){
			//开始拼接sql
			//字段信息
			$f = '';//字段信息的字符串
			if($this->field){
				$f = $this->field;
			}else{
				$f = '*';
			}

			//条件子句
			$w = '';
			if($this->where){
				$w = "where ".$this->where;
			}

			//分页子句
			$l = "";
			if($this->limit){
				$l = "limit ".$this->limit;
			}

			//排序子句
			$o = '';
			if($this->order){
				$o = 'order by '.$this->order;
			}
			//清空相关内容
			$this->clearCondition();

			//拼接sql指令
			$sql = 'select '.$f.' from '.$this->tableName.' '.$w.' '.$o.' '.$l;

			//发送sql指令
			$result = $this->query($sql);
			//判断
			if($result){
				$data = $result->fetchAll();
				//返回结果集
				return $data;
			}else{
				return false;
			}
		} 
		/**
		* 清空搜索的相关子句
		*/
		private function clearCondition(){
			//重置子句成员属性
			$this->field = '';
			$this->where = '';
			$this->order = '';
			$this->limit = '';
		}

		/**
		* 设置字段结果集的信息
		*/
		public function field($field){
			$this->field = $field;
			return $this;
		}

		/**
		* 条件子句设置
		*/
		public function where($where){
			$this->where = $where;
			return $this;
		}

		/**
		* 结果条数的设置
		*/
		public function limit($limit){
			$this->limit = $limit;
			return $this;
		}
		/**
		* 排序的子句
		*/
		public function order($order){
			$this->order = $order;
			return $this;
		}
		/**
		* 获取数据表中所有数据的条数
		*/
		public function count(){
			//sql
			$sql = "select count(*) as total from ".$this->tableName;
			//发送
			$result = $this->query($sql);
			//提取结果集
			$res = $result->fetch();
			// var_dump($res);
			return $res['total'];
		}
	}
 ?>